package com.bawei.gmall.publisher.controller;

import com.alibaba.fastjson.JSON;
import com.bawei.gmall.publisher.bean.Option;
import com.bawei.gmall.publisher.bean.Stat;
import com.bawei.gmall.publisher.service.PublisherService;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName PublisherController
 * @Description TODO
 * @Author mufeng_xky
 * @Date 2020/3/30 11:40
 * @Version V1.0
 **/
@RestController
public class PublisherController {

    @Autowired
    private PublisherService publisherService;

    @GetMapping("realtime-total")
    public String getTotal(@RequestParam("date") String date) {
        List<Map> maps = new ArrayList<>();
        Map dauMap = new HashMap();
        dauMap.put("id", "dau");
        dauMap.put("name", "新增日活");
        dauMap.put("value", publisherService.getDauTotal(date));
        Map newMidMap = new HashMap();
        newMidMap.put("id", "dau");
        newMidMap.put("name", "新增设备");
        newMidMap.put("value", 300);
        Map orderMap = new HashMap();
        orderMap.put("id", "order_amount");
        orderMap.put("name", "新增交易额");
        orderMap.put("value", publisherService.getOrderAmount(date));
        maps.add(dauMap);
        maps.add(newMidMap);
        maps.add(orderMap);
        return JSON.toJSONString(maps);
    }

    //    {"yesterday":{"11":383,"12":123,"17":88,"19":200 },
//        "today":{"12":38,"13":1233,"17":123,"19":688 }}
    @GetMapping("realtime-hour")
    public String getDauHourCount(@RequestParam("id") String id, @RequestParam("date") String date) {
        Map resultMap = new HashMap(2);
        if ("order_amount".equals(id)) {
            Map<String, Double> todayOrderHourAmount = publisherService.getOrderHourAmount(date);
            Map<String, Double> yesterDayOrderHourAmount = publisherService.getOrderHourAmount(getYesterDayString(date));
            resultMap.put("today", todayOrderHourAmount);
            resultMap.put("yesterday", yesterDayOrderHourAmount);
        } else if ("dau".equals(id)) {
            Map<String, Long> todayData = publisherService.getDauHourCount(date);
            Map<String, Long> yesterDayData = publisherService.getDauHourCount(getYesterDayString(date));
            resultMap.put("today", todayData);
            resultMap.put("yesterday", yesterDayData);
        }
        return JSON.toJSONString(resultMap);
    }

    // http://localhost:8070/sale_detail?date=2019-04-01&&startpage=1&size=5&keyword=手机小米
    @GetMapping("sale_detail")
    public String getSaleDetail(@RequestParam("date") String date, @RequestParam("keyword") String keyword,
                                @RequestParam("startpage") int startpage, @RequestParam("size") int size) {
        Map<String, Object> saleDetailMap = publisherService.getSaleDetailFromES(date, keyword, startpage, size);
        Long total = (Long) saleDetailMap.get("total");
        List saleList = (List) saleDetailMap.get("saleList");
        Map genderMap = (Map) saleDetailMap.get("genderMap");
        Map ageMap = (Map) saleDetailMap.get("ageMap");

        // 计算比例
        Long maleCount = (Long) genderMap.get("M");
        Long femaleCount = (Long) genderMap.get("F");
        Double maleRatio = Math.round(maleCount * 1000D / total) / 10D;
        Double femaleRatio = Math.round(femaleCount * 1000D / total) / 10D;

        List genderOptionList = new ArrayList();
        genderOptionList.add(new Option("男", maleRatio));
        genderOptionList.add(new Option("女", femaleRatio));

        Stat genderStat = new Stat("性别占比", genderOptionList);

        // 年龄部分
        Long age_20count = 0L;
        Long age20_30count = 0L;
        Long age_30count = 0L;
        for (Object o : ageMap.entrySet()) {
            Map.Entry entry = (Map.Entry) o;
            String ageString = (String) entry.getKey();
            Long ageCount = (Long) entry.getValue();
            if (Integer.parseInt(ageString) < 20) {
                age_20count += ageCount;
            } else if (Integer.parseInt(ageString) >= 20 && Integer.parseInt(ageString) <= 30) {
                age20_30count += ageCount;
            } else {
                age_30count += ageCount;
            }
        }

        // 计算比率
        Double age_20Ratio = Math.round(age_20count * 1000D / total) / 10D;
        Double age20_30Ratio = Math.round(age20_30count * 1000D / total) / 10D;
        Double age_30Ratio = Math.round(age_30count * 1000D / total) / 10D;

        List ageOptionList = new ArrayList();
        ageOptionList.add(new Option("20岁以下", age_20Ratio));
        ageOptionList.add(new Option("20-30岁", age20_30Ratio));
        ageOptionList.add(new Option("30岁以上", age_30Ratio));

        Stat ageStat = new Stat("年龄段占比", ageOptionList);

        List statList = new ArrayList();
        statList.add(genderStat);
        statList.add(ageStat);

        // 最终结果
        Map finalResultMap = new HashMap();
        finalResultMap.put("total", total);
        finalResultMap.put("stat", statList);
        finalResultMap.put("detail", saleList);

        return JSON.toJSONString(finalResultMap);
    }

    /**
     * 获取前一天日期
     *
     * @param date
     * @return
     */
    private String getYesterDayString(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String yesterDayStr = null;
        try {
            Date today = simpleDateFormat.parse(date);
            Date yesterDay = DateUtils.addDays(today, -1);
            yesterDayStr = simpleDateFormat.format(yesterDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return yesterDayStr;
    }
}
